home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10077 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  52 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Forced override?
  5. Date: 5 Mar 1996 22:44:20 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4hig44$hr9@news1.usa.pipeline.com>
  8. References: <m1ybpgkvpa.fsf@zoger.ipost.com>
  9. NNTP-Posting-Host: pipe16.h1.usa.pipeline.com
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Mar 04, 1996 20:30:57 in article <Forced override?>,
  16. 'bobg@zoger.ipost.com (Bob Glickstein)' wrote: 
  17.  
  18.  
  19. >Quick question: is it possible to declare a virtual member function in 
  20. >such a way that derived classes are *required* to override it?  This 
  21. >would have to work for classes both directly and indirectly derived. 
  22.  
  23. No.  You can only force the function to be defined for each instantiated 
  24. object.  Examples (look at D.  Override inherited from C): 
  25.  
  26. class A { 
  27.  public: virtual int foo() = 0; }; // pure virtual, must be overridden 
  28.  
  29. class B : public A {};     // foo not defined - can't instantiate 
  30.  
  31. class C : public B { 
  32.   public: virtual int foo(); };  
  33. // C is to be instantiated so definition for foo() required 
  34.  
  35. class D : public C 
  36.  { }; 
  37.  
  38.  
  39.  
  40. int main() 
  41.   { 
  42.    B b; // illegal -- no foo 
  43.    C c; // Ok.  obviously 
  44.    D d; // Ok, uses C::foo() 
  45.   return 0; 
  46.   } 
  47.  
  48. -- 
  49. Pete Grant 
  50. Kalevi, Inc. 
  51. Software Engineering & development
  52.